﻿using System.Windows;
using System.Windows.Shapes;

namespace DynamicGeometry
{
    public abstract class CircleBase : ShapeBase<Shape>, ICircle
    {
        [PropertyGridVisible]
        public abstract Point Center
        {
            get;
        }

        [PropertyGridVisible]
        public abstract double Radius
        {
            get;
        }

        public override IFigure HitTest(Point point)
        {
            var width = LogicalWidth();
            if ((Center.Distance(point) - Radius).Abs() < CursorTolerance + width / 2)
            {
                return this;
            }
            return null;
        }

        double LogicalWidth()
        {
            return ToLogical(Shape.StrokeThickness);
        }

        public override void UpdateVisual()
        {
            var center = ToPhysical(Center);
            var diameter = ToPhysical(Radius * 2 + LogicalWidth());
            Shape.Width = diameter;
            Shape.Height = diameter;
            Shape.CenterAt(center);
        }

        public virtual double GetNearestParameterFromPoint(Point point)
        {
            return Math.GetAngle(Center, point);
        }

        public virtual Point GetPointFromParameter(double parameter)
        {
            var center = Center;
            var radius = Radius;
            return new Point(
                center.X + radius * System.Math.Cos(parameter),
                center.Y + radius * System.Math.Sin(parameter));
        }

        public virtual Tuple<double, double> GetParameterDomain()
        {
            return Tuple.Create(0.0, 2 * DynamicGeometry.Math.PI);
        }

        protected override Shape CreateShape()
        {
            return Factory.CreateCircleShape();
        }
    }
}
